package com.quanyan.club.pullclub.utils;

import java.io.IOException;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DES {
    private final static String DES = "DES";
    private final static String ENCODE = "GBK";
    public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
    public static final String defaultKey = "P*_CHAN8";

    /**
     * 默认key加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encrypt(String data) {
        byte[] bt = null;
        try {
            bt = encrypt(defaultKey.getBytes(ENCODE), data.getBytes(ENCODE));
        } catch (Exception e) {
        }
        return new BASE64Encoder().encode(bt);
    }

    /**
     * 指定key加密
     *
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static String encrypt(String key, String data) throws Exception {
        byte[] bt = null;
        try {
            bt = encrypt(key.getBytes(ENCODE), data.getBytes(ENCODE));
        } catch (Exception e) {
        }
        return new BASE64Encoder().encode(bt);
    }

    /**
     * 默认key解密
     *
     * @param data
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data) throws IOException, Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(defaultKey.getBytes(ENCODE), buf);
        return new String(bt, ENCODE);
    }

    /**
     * 指定key解密
     *
     * @param key
     * @param data
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String key, String data) throws IOException, Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(key.getBytes(ENCODE), buf);
        return new String(bt, ENCODE);
    }

    /**
     * DES加密
     *
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] key, byte[] data) throws Exception {
        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        Key secretKey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        IvParameterSpec iv = new IvParameterSpec(key);
        AlgorithmParameterSpec paramSpec = iv;

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        return cipher.doFinal(data);
    }

    /**
     * DES解密
     *
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] key, byte[] data) throws Exception {
        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        Key secretKey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        IvParameterSpec iv = new IvParameterSpec(key);
        AlgorithmParameterSpec paramSpec = iv;

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
        return cipher.doFinal(data);
    }

    public static void main(String[] args) throws Exception {
        System.out.println(com.quanyan.club.pullclub.utils.DES.encrypt("type=1&code=QC123456"));
        System.out.println(com.quanyan.club.pullclub.utils.DES.decrypt(com.quanyan.club.pullclub.utils.DES.encrypt("type=1&code=QC123456")));
    }
}